Parameter Entities

Parameter entities are just like character entities except that they pertain only to the DTD.  In other words, they cannot be used in the document content (elements and attributes).  Parameter entities are like macros and can be used to save on typing and make your DTD’s more readable.
Parameter entities are prefixed with a percent characther (%) and have the following syntax:

	<!ENTITY % entity.name “string literal”>

To illustrate this feature, consider the Employees2.dtd file.

	<!-- Define the element structure --> 
	<!ENTITY % employees.body "employee+">
	<!ENTITY % employee.body "name, position, 
		address, phone">
	<!ENTITY % name.body "first, last">
	<!ENTITY % address.body "street?, city?, state?, 
		zip?">
	<!ENTITY % phone.body "main, office*, fax*, 
		mobile*, home*">

	<!-- Use the element structure -->
	<!ELEMENT employees ( %employees.body; )>

	<!ELEMENT employee ( %employee.body; )>
	<!ATTLIST employee id ID #REQUIRED>

	<!ELEMENT name ( %name.body; )>
	<!ELEMENT first (#PCDATA)>
	<!ELEMENT last  (#PCDATA)>

	<!ELEMENT position (#PCDATA)>

	<!ELEMENT address ( %address.body; )>
	<!ELEMENT street (#PCDATA)>
	<!ELEMENT city   (#PCDATA)>
	<!ELEMENT state  (#PCDATA)>
	<!ELEMENT zip    (#PCDATA)>

	<!ELEMENT phone ( %phone.body; )>
	<!ELEMENT main   (#PCDATA)>
	<!ELEMENT office (#PCDATA)>
	<!ELEMENT fax    (#PCDATA)>
	<!ELEMENT mobile (#PCDATA)>
	<!ELEMENT home   (#PCDATA)>

File: Chapter 6\Employees2.dtd.

The first part of the DTD declare five parameter entities.  These entities define the logical structure of our document.  

	<!ENTITY % employees.body "employee+">
	<!ENTITY % employee.body "name, position, 
		address, phone">
	<!ENTITY % name.body "first, last">
	<!ENTITY % address.body "street?, city?, state?, 
		zip?">
	<!ENTITY % phone.body "main, office*, fax*, 
		mobile*, home*">

In our original DTD (Employees.dtd), this was done inside of the element declaration  We changed each of the element declarations that had parent status so that they used the parameterized entities.

	<!ELEMENT employees ( %employees.body; )>
	<!ELEMENT employee ( %employee.body; )>
	<!ELEMENT name ( %name.body; )>
	<!ELEMENT address ( %address.body; )>
	<!ELEMENT phone ( %phone.body; )>

Tip:	Parameterized entries are restricted for use within a DTD.  
	Character entities are restricted to the content.

There are a couple of things that you must take into account when defining parameter entities.  First, parameter entities declared in the internal subset are available only within the internal subset, even though the internal subset is processed before the external subset.  This means that the scope of parameters internal subset entities are not available in the external subset.

<!DOCTYPE employees SYSTEM “Employees2.dtd” [

	<!—
		The following entity is not available to	
		Employees2.dtd.”  If it was referenced in 
		Employees2.dtd, an error would occur.
	-->
	<!ENTITY local.scope “locally scoped”>
]>

If a parameter entity is declared in the external subset, it is available to both.  This is really useful because you can include other XML declarations (as you will soon see) that contain parameter entities and use them within your internal subset.